<template>
	<view>
		<!-- <view class="search-bar">
			<uni-search-bar cancelButton="none" radius="35" @focus="focus"></uni-search-bar>
		</view> -->
		<my-search @handleClick="gotoSearch()"></my-search>

		<view class="scroll-view-container">
			<!-- 左侧滑动区域 -->
			<scroll-view  class="left-scroll-view" scroll-y = "true" :style="{height:wh + 'px'}" style="width: 300rpx;">
				<block>
					<view v-for="(item,i) in cateList"  :key="i"
					:class="['left-scroll-view-item', i === active ? 'active':'']"
					@click="changeActiveScroll(i)">
					{{item.cat_name}}</view>
				</block>
			</scroll-view>
			<!-- 右边滑动区域 -->
			<scroll-view :scroll-top="scrollTop" class = "right-scroll-view" scroll-y = "true" :style="{height: wh + 'px'}">
				<view v-for="(item2,i2) in cateLevel2" :key="i2" class="right-scroll-view-item-title" >/{{item2.cat_name}}/
					<view class="right-scroll-view-lv3-cate">
						<navigator class="right-scroll-view-lv3-cate-item" v-for="(item3,i3) in item2.children" 
						:key="i3"
						:url="'/subpkg/goods_list/goods_list?cid=' + item3.cat_id">
							<image :src="item3.cat_icon" class="right-scroll-view-lv3-cate-list-img"></image>
							<text>{{item3.cat_name}}</text>
						</navigator>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import { toast } from '../../utils/common';
	
	export default {
		data() {
			return {
				//当前设备可用的高度
				wh: 0,
				cateList: [],
				active: 0,
				//二级分类列表
				cateLevel2: [],
				scrollTop: 0
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight
			this.getCateList()
		},
		methods: {
			async getCateList() {
				const {data:res} = await uni.$http.get('/api/public/v1/categories')
				
				if(res.meta.status !== 200){
					return toast("请求数据失败！")
				}
				this.cateList = res.message
				this.cateLevel2 = res.message[0].children
				console.log(res)
			},
			changeActiveScroll(i){
				this.active = i
				this.cateLevel2 = this.cateList[i].children
				this.scrollTop = this.scrollTop === 0 ? 1:0 
			},
			gotoSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">

.search-bar{
	background-color: #f00000;
	position: sticky;
	top: 0;
	width: 100%;
	z-index: 1000;
}

.scroll-view-container{
	display: flex;
	
	
	.left-scroll-view-item{
		line-height: 100rpx;
		background-color: #f0f0f0;
		text-align: center;
		font-size: 12px;
		
		&.active {
			background-color: #ffffff;
			position: relative;
			
			&::before{
				content: ''; /* 伪元素内容为空 */
				position: absolute; /* 绝对定位 */
				left: 0; /* 在左侧 */
				top: 50%; /* 从顶部开始 */
				transform: translateY(-50%); /* 垂直居中 */
				height: 40rpx; /* 高度填满整个菜单项 */
				width: 4px; /* 竖条的宽度 */
				background-color: #f00000; /* 竖条的颜色 */
			}
			
			
		}
	}
	
	.right-scroll-view{
		.right-scroll-view-item-title{
			text-align: center;
			font-size: 12px;
			font-weight: bold;
			padding: 15px 0;
		}
		.right-scroll-view-lv3-cate{
			display: flex;
			flex-wrap: wrap;
				
			.right-scroll-view-lv3-cate-item{
				width: 33%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				
				text {
					font-size: 12px;
				}
			}
			
			.right-scroll-view-lv3-cate-list-img{
				width: 60px;
				height: 60px;
			}
		}
	}
}

</style>
